package com.maxatime.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.maxatime.dao.EmployeeInfoMapper;
import com.maxatime.dao.EmployeeRegisterMapper;
import com.maxatime.pojo.dto.EmployeeInfoDTO;
import com.maxatime.pojo.entity.EmployeeInfo;
import com.maxatime.pojo.entity.EmployeeRegister;
import com.maxatime.service.EmployeelnfoService;
import com.maxatime.util.PasswordManagerUtil;
import com.maxatime.util.SerialNumberUtil;

@Service
public class EmployeelnfoImpl implements EmployeelnfoService{
	// 日志
		private Logger log = LogManager.getLogger();
	@Autowired
	EmployeeInfoMapper employeeInfoMapper;
	@Autowired
	EmployeeRegisterMapper employeeRegisterMapper;
	
//查询
	@Override
	public List<EmployeeInfoDTO> listData(EmployeeInfoDTO employeeInfoDTO) {
		// TODO Auto-generated method stub
		EmployeeInfo employeeInfo = new EmployeeInfo();
		BeanUtils.copyProperties(employeeInfoDTO, employeeInfo);
		List<EmployeeInfo> list = employeeInfoMapper.listData(employeeInfo);
		List<EmployeeInfoDTO> listDTO = new ArrayList<EmployeeInfoDTO>();
		
		for(int i=0 ; i < list.size();i++) {
			EmployeeInfoDTO emDTO = new EmployeeInfoDTO();
			BeanUtils.copyProperties(list.get(i), emDTO);
			listDTO.add(emDTO);
		}
		
		return listDTO;
	}
	//新增
	@Override
	@Transactional
	public int addData(EmployeeInfoDTO employeeInfoDTO) {
		// TODO Auto-generated method stub
		Date d=new Date();//时间默认值
		EmployeeInfo employeeInfo = new EmployeeInfo();//第一个表
		BeanUtils.copyProperties(employeeInfoDTO, employeeInfo);
		String userid = SerialNumberUtil.getUUID();//自动生成很长的一段随机数,确保userid不重复
		employeeInfo.setUserId(userid);
		employeeInfo.setRemark("*****");//默认备注值为
		employeeInfo.setHiredate(d);
		employeeInfo.setCreateDate(d);
		employeeInfo.setUpdateDate(d);
		employeeInfo.setBirthday(d);
		employeeInfo.setDeleteFlag(Integer.valueOf(0));
		BigDecimal studentsNumber = BigDecimal.valueOf(SerialNumberUtil.getRandomNum());//随机数
		employeeInfo.setSalary(studentsNumber);
		employeeInfoMapper.insertSelective(employeeInfo);
		EmployeeRegister employeeRegister = new EmployeeRegister();//第二个表
		BeanUtils.copyProperties(employeeInfoDTO, employeeRegister);
		String password="123456";
		employeeRegister.setPassword(PasswordManagerUtil.getMd5DigestAsHex(password));//加密
		employeeRegister.setCreateDate(d);
		employeeRegister.setUpdateDate(d);
		employeeRegisterMapper.insertSelective(employeeRegister);
		
		
		return 0;
	}
	//删除
	@Override
	public int deleteByPrimaryKey(Integer pkId) {
		// TODO Auto-generated method stub
		int iRet = employeeInfoMapper.deleteByPrimaryKey(pkId);
		int iRety = employeeRegisterMapper.deleteByPrimaryKey(pkId);//第二个表
		log.info("pkId：： " + pkId);
		return iRet;
	}
	//反显userID
	@Override
	public EmployeeInfoDTO selectByUserId(String userId) {
		// TODO Auto-generated method stub
		EmployeeInfo employeeInfo = employeeInfoMapper.selectByUserId(userId);
		EmployeeInfoDTO employeeInfoDTO = new EmployeeInfoDTO();
		BeanUtils.copyProperties(employeeInfo, employeeInfoDTO);
		
		return employeeInfoDTO;
	}
	//修改  
	@Override
	public int updateByPrimaryKeySelective(EmployeeInfoDTO record) {
		// TODO Auto-generated method stub
		EmployeeInfo employeeInfo = new EmployeeInfo();
		BeanUtils.copyProperties(record, employeeInfo);
		int iRet = employeeInfoMapper.updateByPrimaryKeySelective(employeeInfo);
		return iRet;
	}

}
